package com.lmx.upload;

import com.lmx.bean.UploadUnit;
import com.lmx.util.ConfigUtil;
import com.lmx.util.LogUtil;

import java.io.File;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

public class DeprecatedFileUploadQueue {
    private static final ConcurrentLinkedQueue<UploadUnit> upload_files = new ConcurrentLinkedQueue<>();
    private static final AtomicBoolean enableUpload = new AtomicBoolean(true);
    private static final AtomicBoolean tooManyFilesFlag = new AtomicBoolean(false);
    private static final long max = 1073741824L;//1G
    private static final AtomicLong fileStorage = new AtomicLong(0);


    public static void start() {
        //   LogUtil.log_info("Begin to upload to gateway");

        String filepath = ConfigUtil.getConfig("handle_data.path");
        File file = new File(filepath);
        if (!file.exists()) {
            file.mkdirs();
        }
        File[] files = file.listFiles();


        for (File file1 : files) {
            if (file1.isFile()) {
                synchronized (upload_files) {
                    add(file1, file1.getName(), file1.getPath());
                }
            }
        }

        Thread upload_thread = new Thread(DeprecatedFileUploadQueue::uploadToCloud);
        upload_thread.setName("upload");
        upload_thread.start();
    }

    public static void add(File file, String file_name, String path) {
        if (!file.exists()) {
            return;
        }

        upload_files.offer(new UploadUnit(file, file_name, path));
        //System.out.println(path+":"+file.length());
        if (fileStorage.addAndGet(file.length()) > max && tooManyFilesFlag.compareAndSet(false, true)) {
            LogUtil.log_info("Too many files to upload to gateway, cover old files.");
            while (fileStorage.get() > (max - max / 4)) {
                fileStorage.addAndGet((-upload_files.peek().getFile().length()));
                LogUtil.log_info("Delete " + upload_files.peek().getPath());
                upload_files.poll().getFile().delete();
            }
            tooManyFilesFlag.set(false);
        }

    }

    public static void add(UploadUnit uploadUnit) {
        add(uploadUnit.getFile(), uploadUnit.getFile_name(), uploadUnit.getPath());
    }

    private static void uploadToCloud() {

        UploadUnit uploadUnit = null;
        while (enableUpload.get()) {

            synchronized (upload_files) {
                if (upload_files.isEmpty()) {
                    try {
                        Thread.sleep(1000);
                        continue;
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    uploadUnit = upload_files.poll();
                }
            }

            File file = uploadUnit.getFile();
            if (!file.exists()) {
                continue;
            }

            // System.out.println("upload "+file.getName());

            Client.sendData(uploadUnit);
        }
    }
}
